package patrol.mobile.com.mobilepatrol.utils;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

import java.util.Calendar;

import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.view.CallBack;

public class DatePickerUtil03 {
    /**
     * 测试代码
     */
    private CallBack<DateBean> callBack;
    private static DatePickerUtil03 datePickerUtil;

    public static DatePickerUtil03 getInstance() {
        datePickerUtil = new DatePickerUtil03();
        return datePickerUtil;
    }

    @SuppressLint("NewApi")
    public void show(Context context, final CallBack<DateBean> callBack) {
        this.callBack = callBack;
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dlg = new DatePickerDialog(new ContextThemeWrapper(context,
                android.R.style.Theme_Holo_Light_Dialog_NoActionBar), null, yy, mm, dd) {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                LinearLayout mSpinners = (LinearLayout) findViewById(getContext().getResources().getIdentifier("android:id/pickers", null, null));
                if (mSpinners != null) {
                    NumberPicker mMonthSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/month", null, null));
                    NumberPicker mYearSpinner = (NumberPicker) findViewById(getContext().getResources().getIdentifier("android:id/year", null, null));
                    mSpinners.removeAllViews();
                    if (mYearSpinner != null) {
                        mSpinners.addView(mYearSpinner);
                    }
                    if (mMonthSpinner != null) {
                        mSpinners.addView(mMonthSpinner);
                    }
                }
                View dayPickerView = findViewById(getContext().getResources().getIdentifier("android:id/day", null, null));
                if (dayPickerView != null) {
                    dayPickerView.setVisibility(View.GONE);
                }
            }

            @Override
            public void onDateChanged(DatePicker view, int year, int month, int day) {
                super.onDateChanged(view, year, month, day);

            }

        };
        dlg.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                DateBean dateBean = new DateBean();
                dateBean.setmYear(year);
                String yuefenText;
                if (month < 9) {
                    yuefenText = "0" + (month + 1);
                } else {
                    yuefenText = "" + (month + 1);
                }
                dateBean.setmMonth(yuefenText);
                dateBean.setmDate(year + "-" + dateBean.getmMonth());
                callBack.onBack(dateBean);
            }
        });
        dlg.setTitle("请选择日期");
        dlg.show();
    }

    public void setCallBack(CallBack<DateBean> callBack) {
        this.callBack = callBack;
    }
}
